//
//  HZNormalWebViewController.m
//  app
//
//  Created by 刘华舟 on 15/6/4.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZNormalWebViewController.h"

@interface HZNormalWebViewController()<UIWebViewDelegate, UIScrollViewDelegate>

@property (strong, nonatomic) UIWebView *webView;
@property (strong, nonatomic) UIButton *backBtn;

@property (strong, nonatomic) UIActivityIndicatorView *activity;

@end

@implementation HZNormalWebViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    
    //backBtn
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                            [self.ubo ow:40 oh:40])];
    [self.backBtn setImage:[UIImage imageNamed:kImageMallArrowBackReturnRed] forState:UIControlStateNormal];
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.backBtn addTarget:self action:@selector(backBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBtn];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP oy:self.navigationAndStatusBarHeight], [self.ubo fw:UB_WIDTH fh:UB_HEIGHT oh:-self.navigationAndStatusBarHeight])];
    self.webView.delegate = self;
    self.webView.scrollView.delegate = self;
    self.webView.scrollView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc] initWithView:self.webView viewController:self tryAgain:@selector(refreshData)];
    
    [self.view addSubview:self.webView];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activity.center = self.webView.center;
    self.activity.hidesWhenStopped = YES;
    [self.view addSubview:self.activity];
    
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBar.hidden = NO;
    
    if (self.webTitle.length) {
        self.title = self.webTitle;
    }else{
        self.title = @"";
    }
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webURL]]];
    [self.webView.nothingRefreshView showLoadingMsg:nil errorMsg:@"加载失败,点击重试"];
    
    [self.view bringSubviewToFront:self.activity];
    [self.activity startAnimating];
}

#pragma mark 返回
- (void)backBtnOnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 从新加载页面
- (void)refreshData{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webURL]]];
}

#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [self.activity startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.webView.scrollView endRefreshingAll];
    [self.webView.nothingRefreshView remove];
    [self.activity stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [self.webView.nothingRefreshView activeStop];
    [self.activity stopAnimating];
}

@end
